//
//  LCZFMDetailViewController.m
//  PersonalNews
//
//  Created by Golibyo on 15/11/28.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZFMDetailViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import "LCZTableHeadCell.h"



@interface LCZFMDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)LCZBaseTableView *tableView;




@property(nonatomic, copy)NSString *docid;

@property(nonatomic, strong)LCZBaseImageView *imageView;
@property(nonatomic, strong)LCZBaseImageView *backImageView;
@property(nonatomic, strong)LCZBaseView *backView;

@end

@implementation LCZFMDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"666666666666666%@", self.tid);
    self.view.backgroundColor = [UIColor blackColor];
    [self createTableView];
    [self createData];
    [LCZAVPlayer shareSingleton].frame = CGRectMake(0, 0, WIDTH, HEIGHT / 667.0 * 250);
    [self.view addSubview:[LCZAVPlayer shareSingleton]];
    self.backImageView = [[LCZBaseImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 667.0 * 250)];
    self.backView = [[LCZBaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 667.0 * 250)];
    self.backView.backgroundColor = [UIColor blackColor];
    self.backView.alpha = 0.7;
    [self.backImageView addSubview:self.backView];
    [[LCZAVPlayer shareSingleton] addSubview:self.backImageView];
    self.imageView = [[LCZBaseImageView alloc] initWithFrame:CGRectMake(WIDTH / 2.0 - 90, HEIGHT / 667.0 * 20, WIDTH / 375.0 * 180, WIDTH / 375.0 * 180)];
    self.imageView.layer.cornerRadius = WIDTH / 375.0 * 90;
    self.imageView.layer.masksToBounds = YES;
    [self.backImageView addSubview:self.imageView];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(turnAround:) userInfo:nil repeats:YES];

    
    // Do any additional setup after loading the view.
}

- (void)turnAround:(NSTimer*)timer{
   
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, 0.01);
    
}

- (void)createData {
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/list/%@/0-20.html", self.tid];
    [LCZNetWorkTools getdataWithString:url block:^(id block) {
        NSDictionary *dic = block;
        self.arr = [NSMutableArray array];
        self.arr = dic[self.tid];
        [self.tableView reloadData];
    }];
}

- (void)tableCreateData {
//    [[LCZSingleton shareSingleton].AVPlayerView.player pause];
    [[LCZAVPlayer shareSingleton].player pause];
//    [[LCZAVPlayer shareSingleton] removeFromSuperview];
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", self.docid];
   
    [LCZNetWorkTools getdataWithString:url block:^(id block) {
        NSDictionary *dic = block;
        
        NSString *str = dic[self.docid][@"video"][0][@"url_mp4"];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:dic[self.docid][@"video"][0][@"cover"]]];
        [self.backImageView sd_setImageWithURL:[NSURL URLWithString:dic[self.docid][@"video"][0][@"cover"]]];
        NSLog(@"%@", str);
//        self.imageView.layer.cornerRadius = 90;
//        self.imageView.layer.masksToBounds = YES;
        [LCZAVPlayer shareSingleton].frame = CGRectMake(0, 0, WIDTH, HEIGHT / 667.0 * 250);
//        [[LCZAVPlayer shareSingleton] makeUrl:str];
        [[LCZAVPlayer shareSingleton] changeUrl:str];
        [[LCZAVPlayer shareSingleton].player play];
        }];
}


- (void)createTableView {
    self.tableView = [[LCZBaseTableView alloc] initWithFrame:CGRectMake(0, HEIGHT / 667.0 * 250, WIDTH, HEIGHT / 667.0 * 353) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor yellowColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = HEIGHT / 667.0 * 100;
    [self.tableView registerClass:[LCZTableHeadCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCZTableHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.titleLabel.text = @"少女新闻";
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.arr[indexPath.row][@"imgsrc"]]];
    cell.detailLabel.text = self.arr[indexPath.row][@"title"];
//    NSString *str = [self.arr[indexPath.row][@"ptime"] substringFromIndex:5];
    cell.timeLabel.text = [self.arr[indexPath.row][@"ptime"] substringFromIndex:5];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.docid = self.arr[indexPath.row][@"docid"];
    [self tableCreateData];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
